<template>
  <div class="mod-config">
    <el-row>
      <el-col :span="11">
        <span style="font-size: 14px;font-weight:bold;">酒店名称:{{dataForm.hotelName}}</span>
      </el-col>
      <el-col :span="9">
        <span style="font-size: 14px;font-weight:bold;">地址:{{dataForm.address}}</span>
      </el-col>
      <el-col :span="4">
        <span style="font-size: 14px;font-weight:bold;">电话:{{dataForm.phone}}</span>
      </el-col>
    </el-row>
    <el-row style="margin:10px 0px 10px 0px;">
      <el-col :span="24">
        <span style="font-size: 14px;font-weight:bold;">房型名称:{{dataForm.roomName}}</span>
      </el-col>
    </el-row>
    <el-table :data="dataList" border v-loading="dataListLoading" height="310" style="width: 100%;">
      <el-table-column prop="id" header-align="center" align="center" label="编号" width="50" fixed></el-table-column>
      <el-table-column
        prop="rpCode"
        header-align="center"
        align="center"
        label="编码"
        width="100"
        fixed
      ></el-table-column>
      <el-table-column
        prop="rpName"
        header-align="center"
        align="center"
        label="名称"
        width="160"
        fixed
      ></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="房态" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="success">满房</el-tag>
          <el-tag v-else-if="scope.row.status === 1" size="small" type="success">可预订</el-tag>
          <el-tag v-else size="small" type="warning">不可用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="breakfastNum"
        header-align="center"
        align="center"
        label="早餐个数"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="cancelPolicy"
        header-align="center"
        align="center"
        label="退订政策"
        width="160"
        :formatter="tranCancelPolice"
      ></el-table-column>
      <el-table-column prop="minDays" header-align="center" align="center" label="最小连住" width="100"></el-table-column>
      <el-table-column prop="maxDays" header-align="center" align="center" label="最大连住" width="100"></el-table-column>
      <el-table-column
        prop="minHours"
        header-align="center"
        align="center"
        label="最小提前预定"
        width="140"
      ></el-table-column>
      <el-table-column
        prop="maxHours"
        header-align="center"
        align="center"
        label="最大提前预定"
        width="140"
      ></el-table-column>
      <el-table-column
        prop="isPhone"
        header-align="center"
        align="center"
        label="真实手机号"
        width="100"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isPhone === 0" size="small" type="success">不需要</el-tag>
          <el-tag v-else-if="scope.row.isPhone === 1" size="small" type="warning">需要</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="isCorrect"
        header-align="center"
        align="center"
        label="人工校准"
        width="100"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isCorrect === 0" size="small" type="success">未校准</el-tag>
          <el-tag v-else-if="scope.row.isCorrect === 1" size="small" type="warning">已校准</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="avgPrice"
        header-align="center"
        align="center"
        label="平均价格"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="marketPrice"
        header-align="center"
        align="center"
        label="市场价格"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="isThree"
        header-align="center"
        align="center"
        label="是否第三方"
        width="100"
      ></el-table-column>
      <el-table-column prop="minDays" header-align="center" align="center" label="最小连住" width="100"></el-table-column>
      <el-table-column prop="maxDays" header-align="center" align="center" label="最大连住" width="100"></el-table-column>
      <el-table-column
        prop="minHours"
        header-align="center"
        align="center"
        label="最小提前预定"
        width="140"
      ></el-table-column>
      <el-table-column
        prop="maxHours"
        header-align="center"
        align="center"
        label="最大提前预定"
        width="140"
      ></el-table-column>
      <el-table-column
        prop="services"
        header-align="center"
        align="center"
        label="设施服务"
        width="160"
      ></el-table-column>
      <el-table-column
        prop="startDate"
        header-align="center"
        align="center"
        label="有效期开始"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="endDate"
        header-align="center"
        align="center"
        label="有效期结束"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="startTime"
        header-align="center"
        align="center"
        label="每天生效时间"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="endTime"
        header-align="center"
        align="center"
        label="每天失效时间"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="minAmount"
        header-align="center"
        align="center"
        label="首日入住房间数"
        width="140"
      ></el-table-column>
      <el-table-column
        prop="channel"
        header-align="center"
        align="center"
        label="销售渠道"
      >// 。如需开通，需要申请权限。目前支持的渠道有 H:飞猪 O:钉钉商旅 A:集团内部商旅 M:无线专享价。如果只投放飞猪，改字段不用填写或者只填H；如果有多个用","分开。如果需要投放其他渠道，请联系飞猪运营或者技术支持</el-table-column>
      <el-table-column
        prop="breakfastCal"
        header-align="center"
        align="center"
        label="早餐日历"
        width="160"
      ></el-table-column>
      <el-table-column
        prop="cancelPolicyCal"
        header-align="center"
        align="center"
        label="取消政策日历"
        width="160"
      ></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            v-if="isAuth('ht:htlogicroom:update')"
            @click="addOrUpdateHandle(scope.row.id)"
          >修改</el-button>
          <el-button
            type="text"
            size="small"
            v-if="isAuth('ht:htlogicroom:viewdailroom')"
            @click="viewDailyRoom(dataForm,scope.row)"
          >每日房态</el-button>
          <el-button
            type="text"
            size="small"
            v-if="isAuth('ht:htlogicroom:delete')"
            @click="deleteHandle(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[50]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './htlogicroom-add-or-update'
export default {
  data() {
    return {
      dataForm: {
        hotelId: this.$route.params.hotel.hotelId,
        hotelCode: this.$route.params.hotel.hotelCode,
        hotelName: this.$route.params.hotel.hotelName,
        cityNameZh: this.$route.params.hotel.cityNameZh,
        address: this.$route.params.hotel.address,
        phone: this.$route.params.hotel.phone,
        roomId: this.$route.params.room.id,
        roomCode: this.$route.params.room.roomCode,
        roomName: this.$route.params.room.roomName
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
  },
  methods: {
    viewDailyRoom(roomInfo, rpInfo) {
      this.$router.push({ name: 'ht-htdailyroom', params: { roomInfo: roomInfo, rpInfo: rpInfo } })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/ht/htlogicroom/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'hotelCode': this.dataForm.hotelCode,
          'roomCode': this.dataForm.roomCode
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    tranCancelPolice(row, column) {
      let pText = ""
      try {
        if (row.cancelPolicy === null || row.cancelPolicy === undefined) {
          return pText
        }
        let p = JSON.parse(row.cancelPolicy)
        let pInfo = null
        switch (p.cancelPolicyType) {
          case 1:
            pText = "任意退"
            break
          case 2:
            pText = "不能退"
            break
          case 4:
            pInfo = p.policyInfo
            Object.keys(pInfo).map(key => {
              pText = pText + key + "小时前退订，收取订单总额" + pInfo[key] + "%的违约金。"
            })
            pText = "条件退：" + pText
          case 5:
            let befoTime = 24 - p.policyInfo.timeBefore
            pText = `入住日${befoTime}点前可免费取消`
            pText = "条件退：" + pText
            break
          case 6:
            pInfo = p.policyInfo
            Object.keys(pInfo).map(key => {
              pText = pText + `入住日${(24 - parseInt(key))}点前取消收取前${pInfo[key]}晚房费`
            })
            pText = "条件退：" + pText
            break
        }
      } catch (err) {
        pText = ""
        return pText
      }
      return pText
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/ht/htlogicroom/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
